<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;
use think\Session;

class Kefu extends Common
{
    public function index()
    {
        $userid = input('userid');
        $touserid = input('touserid');

        $data = Db::name('privatechat')->where(['touid' => $touserid, 'uid' => ['neq', $touserid]])->order('isRead')->group('uid')->select();

        $zweidu = Db::name('privatechat')->where(['isRead' => 0, 'touid' => $touserid])->order('isRead')->count();

        foreach ($data as $key => $value) {
            $weidu = Db::name('privatechat')->where(['isRead' => 0, 'touid' => $touserid, 'uid' => $value['uid']])->order('isRead')->count();

            $datass = Db::name('privatechat')->where(['uid' => $value['uid']])->order('create_time desc')->find();

            $data[$key]['shuju'] = $datass;
            $data[$key]['weidu'] = $weidu;
        }

        $this->assign('data', $data);
        $this->assign('zweidu', $zweidu);

        return $this->fetch();
    }

    public function screen()
    {
        $userid = input('userid');
        $touserid = input('touserid');

        //Db::name('privatechat')->where(['touserid' => $userid, 'userid' => $touserid])->setField('isRead', 1);
        $url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '/apis/index.php/home/privatechat/';
        $re = curl(str_replace('/index.php/apis', '/apis',$url) . 'changeread', json_encode(['user_id' => $userid, 'touserid' => $touserid]));

        $data = Db::name('privatechat')->where('userid', ['=', $userid], ['=', $touserid], 'or')->where('touserid', ['=', $userid], ['=', $touserid], 'or')->select();

        $user=DB::name('user')->where('id',$touserid)->find();
        if (!$user) {
            $this->success('当前用户不存在','index/off');
        }

        $user2=DB::name('user')->where('id',$userid)->find();
        if (!$user2) {
            $this->success('当前用户不存在','index/off');
        }

        $count=DB::name('privatechat')->where(function ($query)use($touserid) {
            $query->where('userid',$touserid);
        })->whereOR(function ($query)use($touserid) {
            $query->where('touserid',$touserid);
        })->limit(20)->count();

        $tokenData = array(
            'userid' => $user2['id'],
            'time' => $user2['last_login_time'],
            'sex' => $user2['sex']
        );

        $token = \app\service\Token::createToken($tokenData, 300);
        if($token['status']==200){
            $token = $token['token'];
        }else{
            $token = '';
        }

        $this->assign('token', $token);

        $this->assign('user', $user);
        $this->assign('user2', $user2);
        $this->assign('count',$count);
        $this->assign('data', $data);
        $this->assign('uid', $userid);
        $this->assign('touid', $touserid);
        $this->assign('url', SITE_PATH);

        return $this->fetch();
    }

    public function onchatdata()
    {
        $userid = input('userid');
        $touserid = input('touserid');

        $content = input('content');

        $res = Db::name('privatechat')->insert(['userid' => $userid, 'touserid' => $touserid, 'content' => $content, 'type' => 0, 'create_time' => time()]);

        if ($res) {
            $result = ['code' => 200];
        } else {
            $result = ['code' => 201];
        }

        return json($result);
    }

    //刷新聊天页面
    public function ajaxchat()
    {
        $userid = input('userid');
        $touserid = input('touserid');

        //Db::name('privatechat')->where(['touserid' => $userid, 'userid' => $touserid])->setField('isRead', 1);
        $url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . '/apis/index.php/home/privatechat/';
        curl(str_replace('/index.php/apis', '/apis',$url) . 'changeread', json_encode(['user_id' => $userid, 'touserid' => $touserid]));

        $weidu = Db::name('privatechat')->where(['isRead' => 0, 'userid' => $userid])->count();

        $data = Db::name('privatechat')->where('userid', ['=', $userid], ['=', $touserid], 'or')->where('touserid', ['=', $userid], ['=', $touserid], 'or')->select();

        foreach ($data as $k => $v) {
            //$data[$k]['create_time'] = date('m-d H:i', $v['create_time']);
            if ($v['userid'] == $touserid) {
                $data[$k]['k'] = 0;
            } else {
                $data[$k]['k'] = 1;
            }
        }

        return json(['code' => 200, 'msg' => '查询成功', 'data' => $data, 'weidu' => $weidu]);
    }
}